// PCMod Laptop Case \\

SWEP.PrintName = "Laptop Case"
SWEP.Author = "[GU]|ZLC|thomasfn"
SWEP.Contact = "Don't."
SWEP.Category = "PCMod"
SWEP.Purpose = "To carry laptops around with."
SWEP.Instructions = "Left click to pack away a laptop. Right click to unpack it"

if CLIENT then
	SWEP.Slot 				= 5
	SWEP.DrawAmmo 			= false
	SWEP.DrawCrosshair 		= true
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= false
end

if SERVER then
	AddCSLuaFile("shared.lua")
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= true
	SWEP.AutoSwitchFrom		= false
end

SWEP.ViewModel		= "models/weapons/v_pistol.mdl" 
SWEP.WorldModel		= "models/weapons/w_camphone.mdl"

SWEP.Spawnable = true
SWEP.AdminSpawnable = true

SWEP.HoldType = "normal"

SWEP.Primary.Ammo = "none"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false

SWEP.Secondary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false

if (SERVER) then

	SWEP.ent = nil
	
	function SWEP:Deploy()
		self.Owner:DrawViewModel( false )
		self.Owner:DrawWorldModel( false )
	end
	
	function SWEP:Initialize()
		self:SetWeaponHoldType(self.HoldType)
	end
	
	function SWEP:Think()
		// Should the laptop happen to vanish....
		if ((!self.ent) || (!self.ent:IsValid())) then
			self.ent = nil
			self:SetNWBool( "LaptopHeld", false )
		end
	end
	
	function SWEP:PrimaryAttack()
		if (self.ent) then return end
		
		// Check if we are looking at an entity
		local trace = {}
		trace.start = self.Owner:EyePos()
		trace.endpos = trace.start + self.Owner:GetAimVector() * 200
		trace.filter = self.Owner
		local tr = util.TraceLine(trace)
		
		// Check if that entity is ours - if so, grab the laptop
		if (tr.Entity) then
			if ((tr.Entity.IsPC) && (tr.Entity.IsLaptop)) then
				// Check the PC isn't linked
				local ent = tr.Entity
				if ((ent.Data[ent:EntIndex()].Router) && (!ent.Wireless)) then
					self.Owner:PrintMessage( HUD_PRINTTALK, "Please disconnect the laptop first." )
					return
				end
				self.ent = ent
				self.ent.Entity:SetMoveType( MOVETYPE_NONE )
				self.ent.Entity:SetSolid( SOLID_NONE )
				self.ent.Entity:SetColor( 0, 0, 0, 0 )
				self:SetNWBool( "LaptopHeld", true )
			end
		end
		
		// We were successful
		return true
	end
	
	function SWEP:SecondaryAttack()
		if (!self.ent) then return end
		
		// Get the 'look' trace
		local trace = {}
		trace.start = self.Owner:EyePos()
		trace.endpos = trace.start + self.Owner:GetAimVector() * 50
		trace.filter = self.Owner
		local tr = util.TraceLine(trace)
				
		self.ent.Entity:SetMoveType( MOVETYPE_VPHYSICS )
		self.ent.Entity:SetSolid( SOLID_VPHYSICS )
		self.ent.Entity:SetColor( 255, 255, 255, 255 )
		self.ent.Entity:SetPos( tr.HitPos )
		
		local phys = self.ent.Entity:GetPhysicsObject()
		if (phys && phys:IsValid()) then phys:Wake() end
		
		self.ent = nil
		
		self:SetNWBool( "LaptopHeld", false )
		
		// We were successful
		return true
	end

end

if (CLIENT) then

	function SWEP:DrawHUD()
		local x = ScrW() * 0.80
		local y = ScrH() * 0.95
		local w = ScrW() - x
		local h = ScrH() - y
		surface.SetDrawColor( 50, 50, 50, 200 )
		surface.DrawRect( x, y, w, h )
		local txt = "No Laptop Held"
		if (self:GetNWBool( "LaptopHeld" ) == true) then
			surface.SetDrawColor( 50, 50, 150, 200 )
			surface.DrawRect( x, y, w, h )
			txt = "Laptop Held"
		end
		draw.SimpleText( txt, "PCText", (x+(w/2)), (y+(h/2)), Color(255, 255, 255, 255), 1, 1 )
		surface.SetDrawColor( 50, 50, 50, 255 )
		surface.DrawLine( x, y, x, y+h )
		surface.DrawLine( x, y, x+w, y )
	end

end